using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentOtherSkillTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentOtherSkillTable objects.
    /// </summary>
    public class StudentOtherSkillTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentOtherSkillTable from the underlying datasource.
        /// </summary>
        /// <param name="userAccountKey">The unique UserAccountKey of the StudentOtherSkillTable in the database.</param>
        /// <returns>An StudentOtherSkillTable when the UserAccountKey was found in the database, or null otherwise.</returns>
        public static StudentOtherSkillTable GetItem(int SOSKey, int userAccountKey)
        {
            StudentOtherSkillTable myStudentOtherSkillTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentOtherSkillTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@SOSKey", SOSKey);
                    myCommand.Parameters.AddWithValue("@userAccountKey", userAccountKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentOtherSkillTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentOtherSkillTable;
            }
        }

        /// <summary>
        /// Returns a list with StudentOtherSkillTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentOtherSkillTable objects.</returns>
        public static StudentOtherSkillTableList GetList(int UserAccountKey)
        {
            StudentOtherSkillTableList tempList = new StudentOtherSkillTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentOtherSkillTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StudentOtherSkillTable in the database.
        /// </summary>
        /// <param name="myStudentOtherSkillTable">The StudentOtherSkillTable instance to save.</param>
        /// <returns>The new UserAccountKey if the StudentOtherSkillTable is new in the database or the existing UserAccountKey when an item was updated.</returns>
        public static int Save(StudentOtherSkillTable myStudentOtherSkillTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentOtherSkillTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentOtherSkillTable.SOSKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@SOSKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@SOSKey", myStudentOtherSkillTable.SOSKey);
                    }

                    if (myStudentOtherSkillTable.UserAccountKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", myStudentOtherSkillTable.UserAccountKey);
                    }

                    if (string.IsNullOrEmpty(myStudentOtherSkillTable.SkillName))
                    {
                        myCommand.Parameters.AddWithValue("@skillName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@skillName", myStudentOtherSkillTable.SkillName);
                    }
                    if (string.IsNullOrEmpty(myStudentOtherSkillTable.OrganizationName))
                    {
                        myCommand.Parameters.AddWithValue("@organizationName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@organizationName", myStudentOtherSkillTable.OrganizationName);
                    }
                    if (myStudentOtherSkillTable.NoOfYear < 0)
                    {
                        myCommand.Parameters.AddWithValue("@noOfYear", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@noOfYear", myStudentOtherSkillTable.NoOfYear);
                    }
                    if (string.IsNullOrEmpty(myStudentOtherSkillTable.SkillLevel))
                    {
                        myCommand.Parameters.AddWithValue("@skillLevel", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@skillLevel", myStudentOtherSkillTable.SkillLevel);
                    }
                    if (string.IsNullOrEmpty(myStudentOtherSkillTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myStudentOtherSkillTable.Description);
                    }

                    myCommand.Parameters.AddWithValue("@status", myStudentOtherSkillTable.Status);

                    if (myStudentOtherSkillTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myStudentOtherSkillTable.CreatedDateTime);
                    }
                    if (myStudentOtherSkillTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentOtherSkillTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentOtherSkillTable from the database.
        /// </summary>
        /// <param name="userAccountKey">The UserAccountKey of the StudentOtherSkillTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int userAccountKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentOtherSkillTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@userAccountKey", userAccountKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentOtherSkillTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentOtherSkillTable FillDataRecord(IDataRecord myDataRecord)
        {
            StudentOtherSkillTable myStudentOtherSkillTable = new StudentOtherSkillTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SOSKey")))
            {
                myStudentOtherSkillTable.SOSKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SOSKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
            {
                myStudentOtherSkillTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SkillName")))
            {
                myStudentOtherSkillTable.SkillName = myDataRecord.GetString(myDataRecord.GetOrdinal("SkillName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OrganizationName")))
            {
                myStudentOtherSkillTable.OrganizationName = myDataRecord.GetString(myDataRecord.GetOrdinal("OrganizationName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("NoOfYear")))
            {
                myStudentOtherSkillTable.NoOfYear = myDataRecord.GetByte(myDataRecord.GetOrdinal("NoOfYear"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SkillLevel")))
            {
                myStudentOtherSkillTable.SkillLevel = myDataRecord.GetString(myDataRecord.GetOrdinal("SkillLevel"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myStudentOtherSkillTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStudentOtherSkillTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentOtherSkillTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentOtherSkillTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentOtherSkillTable;
        }
    }

}
